import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-06-24
 * Time: 22:08
 */
public class Sort {
    public static void main(String[] args) {
        int[] arr={9,1,2,5,7,4,8,6,3,5};
        InsertSort(arr);
        System.out.println(Arrays.toString(arr));
        arr= new int[]{9, 1, 2, 5, 7, 4, 8, 6, 3, 5};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
        arr= new int[]{9, 1, 2, 5, 7, 4, 8, 6, 3, 5};
        quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void InsertSort(int[] arr){
        int len=arr.length;
        int j=0;
        for (int i = 1; i <len ; i++) {
            int tmp=arr[i];
            for(j=i-1;j>=0;j--){
                if(arr[j]>tmp){
                    arr[j+1]=arr[j];
                }else{
                    break;
                }
            }
            arr[j+1]=tmp;
        }
    }
    private static void shell(int[] arr,int gap){
        int len=arr.length;
        for (int i = gap; i <len ; i++) {
            int j=i-gap;
            int tmp=arr[i];
            for(;j>=0;j-=gap){
                if(arr[j]>tmp){
                    arr[j+gap]=arr[j];
                }else{
                    break;
                }
            }
            arr[j+gap]=tmp;
        }
    }
    public static void shellSort(int[] arr){
     int gap=arr.length;
     while(gap>0){
         shell(arr,gap);
         gap/=2;
     }
    }
    public static void selectSort(int[] arr){
        int len=arr.length;
        for (int i = 0; i < len; i++) {
            int minIndex=i;
            for (int j = i+1; j <len ; j++) {
                if(arr[j]<arr[minIndex]){
                    minIndex=j;
                }
            }
            swap(arr,i,minIndex);
        }
    }
    public static void swap(int[] arr,int i, int j){
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }
    public static void heapAdjust(int[] arr,int parent,int len){
        int child=parent*2+1;
        while(child<len){
            if(child+1<len&&arr[child]<arr[child+1]){
                child++;
            }
            if(arr[parent]<arr[child]){
                swap(arr,parent,child);
            }else{
                break;
            }
            parent=child;
            child=parent*2+1;
        }
    }
    public static void heapSort(int[] arr){
        int len=arr.length;
        for(int i=(len-1-1)/2;i>=0;i--){
            heapAdjust(arr,i,len);
        }
        for (int i = len-1; i >=0 ; i--) {
            swap(arr,0,i);
            heapAdjust(arr,0,i);
        }
    }
    public static void bubble(int[] arr){
        int len=arr.length;
        for(int i=0;i<len;i++){
            boolean flag=false;
            for(int j=0;j<len-1-i;j++){
                if(arr[j]>arr[j+1]){
                    flag=true;
                    swap(arr,j,j+1);
                }
            }
            if(!flag){
                break;
            }
        }
    }
    private static int partitionPointer(int[] arr,int left,int right){
        int pivot=arr[left];
        int d=left+1;
        for(int i=left+1;i<=right;i++){
            if(arr[i]<pivot){
                swap(arr,i,d);
                d++;
            }
        }
        swap(arr,d-1,left);
        return d-1;
    }
    private static int partitionHole(int[] arr,int left,int right){
        int pivot=arr[left];
        while(left<right){
            while(left<right&&arr[right]>=pivot){
                right--;
            }
            arr[left]=arr[right];
            while(left<right&&arr[left]<=pivot){
                left++;
            }
            arr[right]=left;
        }
        arr[right]=pivot;
        return left;
    }
    public static int partitionHore(int[] arr,int left,int right){
        int pivot=arr[left];
        int i=left;
        while(left<right){
            while(left<right&&arr[right]>=pivot){
                right--;
            }
            while(left<right&&arr[left]<=pivot){
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,i,left);
        return left;
    }
    private static int midOfThree(int[] arr,int left,int right){
        int mid=left+((right-left)>>>1);
        if(arr[left]<arr[right]){
            if(arr[mid]<arr[left]){
                return left;
            }
            else if(arr[mid]>arr[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(arr[mid]<arr[right]){
                return right;
            }
            else if(arr[mid]>arr[left]){
                return left;
            }else{
                return mid;
            }
        }
    }
    public static void quick(int[] arr,int left,int right){
        if(left>=right){
            return;
        }
        int pivot=partitionHore(arr,left,right);
        quick(arr,left,pivot-1);
        quick(arr,pivot+1,right);
    }
    public static void quickSort(int[] arr){
        quick(arr,0,arr.length-1);
    }
    public static void quickNor(int[] arr){
        Stack<Integer> stack=new Stack<>();
        int left=0;
        int right=arr.length-1;
        stack.push(left);
        stack.push(right);
        while(!stack.isEmpty()){
            right=stack.pop();
            left=stack.pop();
            int pivot=partitionHore(arr,left,right);
            if(left+1<pivot){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1<right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }
    private static void merge(int[] arr,int left,int mid,int right){
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int k=0;
        int[] tmp=new int[right-left+1];
        while(s1<=e1&&s2<=e2){
            if(arr[s1]<arr[s2]){
                tmp[k++]=arr[s1++];
            }
            else{
                tmp[k++]=arr[s2++];
            }
        }
        while(s1<=e1){
            tmp[k++]=arr[s1++];
        }
        while(s2<=e2){
            tmp[k++]=arr[s2++];
        }
        for(int i=0;i<k;i++){
            arr[i+left]=tmp[i];
        }
    }
    public static void mergeSort(int[] arr,int left,int right){
        if(left>=right){
            return;
        }
        int mid=left+((right-left)>>>1);
        mergeSort(arr,left,mid);
        mergeSort(arr,mid+1,right);
        merge(arr,left,mid,right);
    }
    public static void mergeSortNor(int[] arr){
        int len=arr.length;
        int gap=1;
        while(gap<len){
            for(int i=0;i<len;i+=gap*2){
                int left=i;
                int mid=left+gap-1;
                if(mid>=len){
                    mid=len-1;
                }
                int right=mid+gap;
                if(right>=len){
                    right=len-1;
                }
                merge(arr,left,mid,right);
            }
            gap*=2;
        }
    }
}
